<?php

namespace app\index\controller;

use app\index\model\User;
use think\Controller;
use think\Session;

class Register extends Base
{
    public function index()
    {
        $user = User::where('openid', $this->wechat_user['openid'])->find();
        if ($user) {
            $this->redirect('index/register/ok');
        }
        return $this->fetch();
    }

    public function add()
    {
        $param = input('post.');

        User::create([
            'nickname' => $this->wechat_user['nickname'] ?? '',
            'openid' => $this->wechat_user['openid'],
            'headimgurl' => $this->wechat_user['headimgurl'] ?? '',
            'name' => $param['name'],
            'age' => intval($param['age']),
            'mobile' => intval($param['mobile'] ?? ''),
            'sex' => intval($param['sex']),
            'status' => User::STATUS_WAIT
        ]);
        return $this->redirect('index/register/ok');
        // return json(['code' => 1, 'data' => '', 'msg' => '注册成功']);
    }

    public function ok()
    {
        $user = User::where('openid', $this->wechat_user['openid'])->find();
        if (!$user) {
            $this->redirect('index/register/index');
        }

        $this->assign('user', $user);
        return $this->fetch();
    }
}
